package com.baosight.sczx.zm.cg.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.ch.domain.ZMCHProduceStatistics;
import com.baosight.sczx.zm.cp.domain.ZMCPProfessionalPlan;
import com.baosight.sczx.zm.cp.domain.ZMCPYearPlanZB;
/**
 * 原煤产量
 */
public class ServiceZMCG91 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);

		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdProjectName", "原煤产量", true);
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdMiningName", UserSession.getInSessionProperty("miningName"), true);
		
		EiInfo outInfo = super.query(inInfo, "ZMCHProduceStatistics.query", new ZMCHProduceStatistics());
		outInfo.addBlock(getMines());
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
        outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCHImportFaceReport.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCHProduceStatistics _dom = new ZMCHProduceStatistics();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			String fdDepartmentName = _dom.getFdDepartmentName();
			String fdVirtualDate = _dom.getFdVirtualDate();
			String year = StaticTools.getYearStr(fdVirtualDate);
			String lastYear = String.valueOf(Integer.parseInt(year) - 1);
			String month = StaticTools.getMonthStr(fdVirtualDate);
			
			//月计划
			String fdPlanYear;//采煤计划年份
			String fdPlanMonth;//采煤计划月份
			BigDecimal fdYmSum = new BigDecimal("0");//采煤月计划产量
			BigDecimal day = new BigDecimal("30");
			String fdMiningId;//采煤计划上报单位
			params.put("fdPlanYear", year);
			params.put("fdPlanMonth", month);
			params.put("fdMiningId", fdDepartmentName);
			List list = dao.query("ZMCPProfessionalPlan.query1",params);
			if(list!= null && list.size()>0){
				ZMCPProfessionalPlan bean = (ZMCPProfessionalPlan)list.get(0);
				fdYmSum = bean.getFdYmSum();
			}
			
			//年计划
			String fdYearGetway;//采煤年计划年份
			String fdForeignId;//采煤年计划上报单位miningID
			double fdSurplusLength = 0;//原煤年生产总量(万t)
			BigDecimal fdSurplusLength1 = null;
			params.put("fdYearGetway", year);
			params.put("fdForeignId", fdDepartmentName);
			List list1 = dao.query("ZMCPYearPlanZB.query1",params);
			if(list1!= null && list1.size()>0){
				ZMCPYearPlanZB bean1 = (ZMCPYearPlanZB)list1.get(0);
				//fdSurplusLength = bean1.getFdSurplusLength() * 10000;
				fdSurplusLength1 = new BigDecimal(fdSurplusLength);
			}
			
//			//上年计划
//			BigDecimal fdSurplusLengthLest = null;
//			params.put("fdYearGetway", lastYear);
//			params.put("fdForeignId", fdDepartmentName);
//			List list2 = dao.query("ZMCPYearPlanZB.query1",params);
//			if(list2!= null && list2.size()>0){
//				ZMCPYearPlanZB bean2 = (ZMCPYearPlanZB)list2.get(0);
//				fdSurplusLength = bean2.getFdSurplusLength() * 10000;
//				fdSurplusLengthLest = new BigDecimal(fdSurplusLength);
//			}
			boolean validate = validate(inInfo, _dom,"ZMCHProduceStatistics.validate",params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}else{
			_dom.setFdMonthPlan(fdYmSum);//月计划
			_dom.setFdDayPlan(fdYmSum.divide(day,2));//日计划
			_dom.setFdYearPlan(fdSurplusLength1);//年计划
		    _dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_PRODUCE_STATISTICS"));
		    _dom.setFdProjectName("原煤产量");
			this.dao.insert("ZMCHProduceStatistics.insert",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCHProduceStatistics _dom = new ZMCHProduceStatistics();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			String fdDepartmentName = _dom.getFdDepartmentName();
			String fdVirtualDate = _dom.getFdVirtualDate();
			String year = StaticTools.getYearStr(fdVirtualDate);
			String lastYear = String.valueOf(Integer.parseInt(year) - 1);
			String month = StaticTools.getMonthStr(fdVirtualDate);
			
			//月计划
			String fdPlanYear;//采煤计划年份
			String fdPlanMonth;//采煤计划月份
			BigDecimal fdYmSum = new BigDecimal("0");//采煤月计划产量
			BigDecimal day = new BigDecimal("30");
			String fdMiningId;//采煤计划上报单位
			params.put("fdPlanYear", year);
			params.put("fdPlanMonth", month);
			params.put("fdMiningId", fdDepartmentName);
			List list = dao.query("ZMCPProfessionalPlan.query1",params);
			if(list!= null && list.size()>0){
				ZMCPProfessionalPlan bean = (ZMCPProfessionalPlan)list.get(0);
				fdYmSum = bean.getFdYmSum();
			}
			
			//年计划
			String fdYearGetway;//采煤年计划年份
			String fdForeignId;//采煤年计划上报单位miningID
			double fdSurplusLength = 0;//原煤年生产总量(万t)
			BigDecimal fdSurplusLength1 = null;
			params.put("fdYearGetway", year);
			params.put("fdForeignId", fdDepartmentName);
			List list1 = dao.query("ZMCPYearPlanZB.query1",params);
			if(list1!= null && list1.size()>0){
				ZMCPYearPlanZB bean1 = (ZMCPYearPlanZB)list1.get(0);
				//fdSurplusLength = bean1.getFdSurplusLength() * 10000;
				fdSurplusLength1 = new BigDecimal(fdSurplusLength);
			}
			
//			//上年计划
//			BigDecimal fdSurplusLengthLest = null;
//			params.put("fdYearGetway", lastYear);
//			params.put("fdForeignId", fdDepartmentName);
//			List list2 = dao.query("ZMCPYearPlanZB.query1",params);
//			if(list2!= null && list2.size()>0){
//				ZMCPYearPlanZB bean2 = (ZMCPYearPlanZB)list2.get(0);
//				fdSurplusLength = bean2.getFdSurplusLength() * 10000;
//				fdSurplusLengthLest = new BigDecimal(fdSurplusLength);
//			}
			
			boolean validate = validate(inInfo, _dom,"ZMCHProduceStatistics.validate",params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}else{
			_dom.setFdMonthPlan(fdYmSum);//月计划
			_dom.setFdDayPlan(fdYmSum.divide(day,2));//日计划
			_dom.setFdYearPlan(fdSurplusLength1);//年计划
			this.dao.update("ZMCHProduceStatistics.update",_dom);
			}
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCHProduceStatistics _dom = new ZMCHProduceStatistics();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCHProduceStatistics.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCHProduceStatistics _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdDepartmentName = _dom.getFdDepartmentName();
		String fdVirtualDate = _dom.getFdVirtualDate();
		params.put("fdDepartmentName", fdDepartmentName);
		params.put("fdVirtualDate", fdVirtualDate);
		List result = dao.query("ZMCHProduceStatistics.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每矿每天只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	
	}	
} 